package com.gis3c.sys.service.impl;

import com.gis3c.sys.dao.AppDao;
import com.gis3c.sys.dao.MenuDao;
import com.gis3c.sys.entry.po.App;
import com.gis3c.sys.entry.po.Menu;
import com.gis3c.sys.entry.vo.AppModel;
import com.gis3c.sys.service.AppService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by hukekuan on 18-3-18.
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class AppServiceImpl implements AppService {
    @Autowired
    private Mapper baseMapper;
    @Autowired private AppDao appDao;
    @Autowired private MenuDao meuDao;

    @Override
    public List<AppModel> findAppList() {
        List<AppModel> result = new ArrayList<>();
        List<App> appList = appDao.findAppList();
        appList.forEach(app -> result.add(baseMapper.map(app,AppModel.class)));
        return result;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public Integer createApp(AppModel appModel) {
        App newApp = baseMapper.map(appModel,App.class);
        newApp.setAppId(UUID.randomUUID().toString());
        newApp.setCreatedTime(new Date());
        newApp.setUpdateTime(new Date());

        return appDao.createApp(newApp);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void updateApp(AppModel appModel) {

    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public Integer deleteApp(String appId) {
        meuDao.deleteMensByAppId(appId);
        appDao.uncorrelationAppByAppId(appId);
        Integer deleteIndex = appDao.deleteApp(appId);

        return deleteIndex;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public int daleteApps(String... appIds) {
        return 0;
    }

    @Override
    public AppModel findAppById(String appId) {
        return null;
    }
}
